<?php

namespace app\sys\api;

use app\common\logic\Config;

class Setting extends Common
{
    /**
     * 更新
     * @return array|string
     * @author wb <pithyone@vip.qq.com>
     */
    public function update()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $req = input('');

        //验证数据
        $validate = validate('Config');
        if ($validate->check($req) === false)
            return $this->error($validate->getError());

        $name = $req['name'];
        $value = $req['value'];

        $ret = Config::update(['value' => $value], ['name' => $name]);

        if ($ret === false)
            return $this->error('更新失败');

        return $this->success('更新成功');
    }

    /**
     * 详情
     * @return array|string
     * @author wb <pithyone@vip.qq.com>
     */
    public function get()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $req = input('');

        if (!isset($req['name']) || empty($req['name']))
            return $this->error('请选择要操作的配置项');

        $ret = Config::get(['name' => $req['name']]);
        if ($ret === false)
            return $this->error('配置不存在');

        return $this->success($ret);
    }

    /**
     * 列表
     * @return array|string
     * @author wb <pithyone@vip.qq.com>
     */
    public function lists()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $ret = Config::all();
        return $this->success($ret);
    }
}